package com.sy.muldatasource.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sy.muldatasource.dao.one.TestS1Reposity;
import com.sy.muldatasource.dao.two.TestS2Reposity;
import com.sy.muldatasource.pojo.one.TestS1;
import com.sy.muldatasource.pojo.two.TestS2;

@Service
public class TestService {
	@Autowired
	private TestS1Reposity s1Reposity;
	@Autowired
	private TestS2Reposity s2Reposity;
	/**
	 * transactionManager 指定使用的事务管理器，默认为transactionManager
	 * timeout 设置超时时间，默认-1永不超时。
	 */
	@Transactional(transactionManager="transactionManager")
	public void saveSuccess() {
		Optional<TestS2> t2 = s2Reposity.findById(1);
		TestS2 ts2 = t2.get();
		ts2.setName("哈哈bb2");
		
		Optional<TestS1> t1 = s1Reposity.findById(1);
		TestS1 ts1 = t1.get();
		ts1.setName("哈哈aa1");
		
		s2Reposity.save(ts2);
		s1Reposity.save(ts1);
		
	}
	
	@Transactional
	public void saveError() {
		Optional<TestS1> t1 = s1Reposity.findById(1);
		TestS1 ts1 = t1.get();
		ts1.setName("哈哈32");
		s1Reposity.save(ts1);
		
		Optional<TestS2> t2 = s2Reposity.findById(1);
		TestS2 ts2 = t2.get();
		ts2.setName("哈哈41");
		s2Reposity.save(ts2);
		
		throw new RuntimeException();		
	}
	
}
